#include "user.h"

User::User() {}
QJsonObject User::toJson(){
    QJsonObject item;
    item["name"] = name;
    item["idcardnumber"] = idcard_num;
    item["age"] = QString::number(age);
    item["email"] = email;
    item["role"] = typeToString(type);
    item["password"] = pass;
    return item;
}
User User::fromJson(QJsonObject t){
    QString name = t["Name"].toString();
    QString id = t["Id"].toString();
    QString IdCardNumber = t["IdCardNumber"].toString();
    int age = t["Age"].toInt();
    QString Email = t["Email"].toString();
    QDate join_at = QDate::fromString(t["createdAt"].toString());
    Role r = StringTotype(t["Role"].toString());
    QString pass = t["PassWord"].toString();
    return User(name,pass,IdCardNumber,Email,r,age,join_at);
}
QString User::typeToString(Role f){
    if(f==student)return "Student";
    if(f==adult)return "Adult";
    if(f==elderly)return "Elderly";
    if(f==child)return "Child";
    return "UnKnown";
}
Role User::StringTotype(QString s){
    if(s=="Student")return student;
    if(s=="Adult")return adult;
    if(s=="Elderly")return elderly;
    if(s=="Child")return child;
    return adult;
}
